package com.hangzhou.xyj.ui.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by neo.duan on 2015/12/22.
 * 名家名团dapter
 */
public class FamousAdapter extends BaseAdapter {

    private Context mContext;
    private List<UserModel> mData;

    public FamousAdapter(Context context, List<UserModel> data) {
        this.mContext = context;
        if (data == null || data.isEmpty()) {
            mData = new ArrayList<>();
        }
        this.mData = data;
    }

//    public void update(List<UserModel> list) {
//        if (list == null) {
//            list = new ArrayList<>();
//        }
//        this.mData = list;
//        notifyDataSetChanged();
//    }

    @Override
    public int getCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.layout_famous_item, null);
            holder = new ViewHolder();
            holder.mTvDrama = (MyTextView) convertView.findViewById(R.id.tv_drama_item);
            holder.mIvDrama = (CircleImageView) convertView.findViewById(R.id.iv_drama_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //最后一个:更多
        if (getCount() >= 8 && position == (getCount() - 1)) {
            holder.mIvDrama.setImageResource(R.drawable.ic_more_expert);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(DensityUtil.dip2px(mContext, 23), DensityUtil.dip2px(mContext, 23));
            holder.mIvDrama.setLayoutParams(lp);
            holder.mTvDrama.setText("更多");
        } else {
            holder.mTvDrama.setText(mData.get(position).getName());
            holder.mIvDrama.displayImage(mData.get(position).getHeadPic());
        }

        return convertView;
    }

    static class ViewHolder {
        private TextView mTvDrama;
        private CircleImageView mIvDrama;
    }
}
